import { host } from './config.js'
var navigator = weex.requireModule('navigator');
var progress = weex.requireModule("progress")
let net = {

  postShort: function (weg, param, header, start, success, compelete) {
    var modal = weex.requireModule("modal")
    this.postFull(weg, param, header, start, success, function (res) {
      //fail
      modal.toast({
        message: res.msg
      })
    }, function () {
      //exception
      modal.toast({
        message: '网络异常！'
      })
    }, function () {
      //compelete

      compelete();
    })

  },

  postFull: function (weg, param, header, start, success, fail, exception, compelete) {
    var net = weex.requireModule("net")
    var modal = weex.requireModule("modal")
    var pref = weex.requireModule('pref');
    var st = weex.requireModule('static');
    var self = this;
    var url = host + weg;
    var locale = pref&&pref.getString('locale')
    var token = st.getString('token')
    if (token != undefined && token != '') {
      header.token = token
    }
    if(locale){
      header.locale = locale
    }
    // param.token='95d594d7b18fd1c7db37e81dd5bae9c9'
    net.postJson(url, param, header, function () {
      //start
      start();
    }, function (e) {
      //success
      // modal.toast({message:e.res.err})
      if (e.res&&e.res.code == 0) {
        success(e.res)
      } else {
        // modal.toast({message:e.res.msg})
        if (token != undefined && token != '') {
          st.remove('token')
          return;
        }
        if (e.res&&e.res.err == 1000) {
     
        } else
          {
             fail(e.res);
          }
      }

    }, function (e) {
      //compelete


      compelete();

    }, function (e) {
      // exception
      exception();
    });

  },

  post: function (weg, param, success) {
    
    this.postShort(weg, param, {}, function () {
      progress.show();
    }, success, function () {
      progress.dismiss();
    });
  },
  get: function (weg,param, success) {
    var net = weex.requireModule("net")
    var pref = weex.requireModule('pref');
    var header = {}
    var self = this;
    var url = host + weg;
    var locale = pref&&pref.getString('locale')
    if (locale) {
      header.locale = locale
    }
    var progress = weex.requireModule("progress")
    progress.show();
    net.get(url, param, header, function () {
     
    }, function(e){
      success(e.res)
    }, function () {
      progress.dismiss();
    },function(){
      
    });
  },
  postSilent: function (weg, param, success) {

    this.postFull(weg, param, {}, function () {

    }, success, function (res) {
      //fail

    }, function () {
      //exception

    }, function () {
      //compelete


    })
  },
  getSilent: function (weg, param, success, compelete) {
    var net = weex.requireModule("net")
    var pref = weex.requireModule('pref');
    var url = host + weg;
    var header = {}
     var locale = pref&&pref.getString('locale')
     if (locale) {
       header.locale = locale
     }
    net.get(url, param, header, function () {

    }, function (e) {
      //fail
      success(e.res)

    }, function () {
      //exception
      if(compelete)compelete()
    }, function () {
      //compelete
      // compelete()

    })
  },
  uploadFile: function (weg, param,path, success) {
    var net = weex.requireModule("net")
    var progress = weex.requireModule("progress")
    let header = {}
    var url = host + weg
    net.postFile(url, param, header, path, () => {
      progress.show()
    }, (e) => {
      success(e.res)
      var modal = weex.requireModule("modal")
      modal.toast({
        message: '上传成功！'
      })
    }, () => {
      //compelete
       progress.dismiss();
    }, () => {
      //exception
       progress.dismiss();

      var modal = weex.requireModule("modal")
      modal.toast({
        message: '上传异常！'
      })
    })
  }

}
module.exports = net